﻿package com.superwindcloud.blackjack

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import com.superwindcloud.blackjack.ui.theme.BlackJackTheme
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.draw.paint
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.view.WindowCompat

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        WindowCompat.setDecorFitsSystemWindows(window, false)
        setContent {
            BlackJackTheme {
                Scaffold(
                    modifier = Modifier
                        .fillMaxSize()
                        .background(Color.Blue)
                ) { innerPadding ->
                    AppContent(modifier = Modifier.padding(innerPadding))
                }
            }
        }
    }

    @Composable
    private fun AppContent(modifier: Modifier) {

        val isGameStarted = remember { mutableStateOf(false) }
        val rememberRemainingChips = remember { mutableStateOf(3000) }
        val rememberChipValue = remember { mutableStateOf(5) }


        Box(
            modifier = Modifier
                .fillMaxSize()
                .paint(
                    painter = painterResource(id = R.drawable.bg),
                    contentScale = ContentScale.FillBounds
                )
        ) {

            HelpDialog()
            Image(
                painter = painterResource(id = R.drawable.rule1),
                contentDescription = "rules",
                modifier = Modifier
                    .align(Alignment.Center)
                    .padding(top = 16.dp, end = 16.dp, bottom = 30.dp)
                    .fillMaxSize()
            )
            Button(
                modifier = Modifier
                    .align(Alignment.TopEnd)
                    .padding(bottom = 30.dp)
                    .offset(y = (70).dp),
                shape = RoundedCornerShape(16.dp),
                content = {
                    Text(
                        text = "重新开始",
                        fontSize = 20.sp,
                        fontWeight = FontWeight.Bold,
                        fontStyle = FontStyle.Italic,
                        color = Color.White
                    )
                },
                onClick = {
                    isGameStarted.value = false
                    rememberChipValue.value = 5
                },
            )
            bargainingChip(rememberChipValue, rememberRemainingChips)
            if (!isGameStarted.value) {
                ShowStartGameTips()
                startButton(isGameStarted, rememberChipValue)
            }
            if (isGameStarted.value) {
                ShowBetAmountTips()
                Log.d("MainActivity", "game started")
                val isStartedDealingCards = remember { mutableStateOf(false) }
                if (!isStartedDealingCards.value) {
                    startDealingCardsButton(isStartedDealingCards)
                    selectBetAmount(rememberChipValue, rememberRemainingChips)
                }

                if (isStartedDealingCards.value == true && rememberRemainingChips.value > 0) {

                    // 先给庄家发牌，然后玩家操作
                    var dealerInitScore  = remember { mutableStateOf(0) };
                    val  isDealerTurnToAskForCards  = remember { mutableStateOf(false) }
                    val PlayerCurrentScore = remember { mutableStateOf(0) }
                    val  theDealerHasFinishedTakingTheCards  = remember { mutableStateOf(false) }
                    dealingCardsForDealer( rememberChipValue, rememberRemainingChips, isGameStarted ,dealerInitScore
                    ,isDealerTurnToAskForCards )
                    playerBettingOperationInterface(  rememberChipValue, rememberRemainingChips  ,
                        isGameStarted , dealerInitScore , isDealerTurnToAskForCards, PlayerCurrentScore,
                        theDealerHasFinishedTakingTheCards)
                    // 玩家操作完毕，判断胜利条件
                      DetermineTheWinnerOfTheBankerAndPlayer( theDealerHasFinishedTakingTheCards,  rememberChipValue, rememberRemainingChips  ,dealerInitScore ,
                          PlayerCurrentScore )
                }

            }

        }
    }


    @Composable
    private fun startDealingCardsButton(isStartedDealingCards: MutableState<Boolean>) {
        Box(modifier = Modifier.fillMaxSize()) {

            Button(
                modifier = Modifier
                    .align(Alignment.Center)
                    .padding(bottom = 30.dp),

                shape = RoundedCornerShape(16.dp),
                content = {
                    Text(
                        text = "开始发牌",
                        fontSize = 20.sp, fontWeight = FontWeight.Bold,
                        fontStyle = FontStyle.Italic, color = Color.White
                    )
                },
                onClick = {
                    Log.d("MainActivity", "start dealing cards button clicked")
                    isStartedDealingCards.value = true
                }
            )


        }

    }

    @Composable
    private fun ShowBetAmountTips() {
        Box(
            modifier = Modifier
                .fillMaxSize()
        ) {
            Image(
                painter = painterResource(id = R.drawable.alert_bg),
                contentDescription = "Logo",
                modifier = Modifier
                    .align(Alignment.TopCenter)
                    .padding(top = 16.dp, end = 16.dp, bottom = 30.dp)
                    .size(128.dp)
            )
            Image(
                painter = painterResource(id = R.drawable.bet_please),
                contentDescription = "Logo",
                modifier = Modifier
                    .align(Alignment.TopCenter)
                    .padding(top = 16.dp, end = 16.dp, bottom = 30.dp)
                    .size(128.dp)
            )
        }

    }

    @Composable
     private fun ShowStartGameTips() {
        Box(
            modifier = Modifier
                .fillMaxSize()
        ) {


            Image(
                painter = painterResource(id = R.drawable.alert_bg),
                contentDescription = "Logo",
                modifier = Modifier
                    .align(Alignment.TopCenter)
                    .padding(top = 16.dp, end = 16.dp, bottom = 30.dp)
                    .size(128.dp)
            )
            Text(
                text = stringResource(id = R.string.start_game_tips),
                modifier = Modifier
                    .align(Alignment.TopCenter)
                    .offset(y = (50).dp)
                    .padding(top = 16.dp, end = 16.dp, bottom = 30.dp),
                fontSize = 20.sp,
                fontWeight = FontWeight.Bold,
                fontStyle = FontStyle.Italic,
                color = Color.White
            )

        }

    }

    @Composable
    private fun selectBetAmount(rememberChipValue: MutableState<Int>, rememberRemainingChips: MutableState<Int>) {
        Box(
            modifier = Modifier
                .fillMaxSize()
                .offset(y = (-200).dp),
            contentAlignment = Alignment.Center
        ) {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .align(Alignment.BottomStart)
                    .padding(start = 20.dp, bottom = 30.dp),
                horizontalArrangement = Arrangement.SpaceBetween, // 使图片均匀分布
                verticalAlignment = Alignment.CenterVertically // 垂直居中
            ) {
                Image(
                    painter = painterResource(id = R.drawable.score5),
                    contentDescription = "Score 5",
                    modifier = Modifier
                        .size(50.dp)
                        .clickable(
                            enabled = true,
                            onClick = {
                                rememberChipValue.value = 5
                            }
                        ),
                    contentScale = ContentScale.Fit,


                    )
                Image(
                    painter = painterResource(id = R.drawable.score10),
                    contentDescription = "Score 10",
                    modifier = Modifier
                        .size(50.dp)
                        .clickable {
                            rememberChipValue.value = 10
                        },
                    contentScale = ContentScale.Fit
                )
                Image(
                    painter = painterResource(id = R.drawable.score25),
                    contentDescription = "Score 25",
                    modifier = Modifier
                        .size(50.dp)
                        .clickable {
                            rememberChipValue.value = 25
                        },
                    contentScale = ContentScale.Fit

                )
                Image(
                    painter = painterResource(id = R.drawable.score100),
                    contentDescription = "Score 100",
                    modifier = Modifier
                        .size(50.dp)
                        .clickable(
                            enabled = true, onClick = {
                                rememberChipValue.value = 100
                            }
                        ), contentScale = ContentScale.Fit
                )
                Image(
                    painter = painterResource(id = R.drawable.score500),
                    contentDescription = "Score 500",
                    modifier = Modifier
                        .size(50.dp)
                        .clickable {
                            rememberChipValue.value = 500
                        }, contentScale = ContentScale.Fit
                )
            }
        }
    }

    @Composable
    private  fun startButton(isGameStarted: MutableState<Boolean>, rememberRemainingChips: MutableState<Int>) {

        Box(
            modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center
        ) {
            Image(
                painter = painterResource(id = R.drawable.start),
                contentDescription = "Custom Icon",
                modifier = Modifier
                    .align(Alignment.Center)
                    .padding(top = 16.dp, end = 16.dp, bottom = 30.dp)
                    .size(128.dp)
                    .clickable {
                        isGameStarted.value = !isGameStarted.value

                        Log.d("MainActivity", "start button clicked")
//                        rememberRemainingChips.value += 1000
                    }
            )

        }

    }

    @Composable
    private   fun bargainingChip(rememberChipValue: MutableState<Int>, rememberRemainingChips: MutableState<Int>) {

        Box(
            modifier = Modifier
                .fillMaxSize()
                .offset(y = (-50).dp),
            contentAlignment = Alignment.BottomEnd
        ) {
            Row(
                modifier = Modifier
                    .align(Alignment.BottomStart),
            ) {
                Image(
                    painter = painterResource(id = R.drawable.coin),
                    contentDescription = "Custom Icon",
                    modifier = Modifier
                        .padding(end = 5.dp)  // 确保图片和文本之间有适当的间距
                        .size(64.dp)
                        .offset(y = (-50).dp)
                )
                Text(
                    text = "底池$${rememberChipValue.value}",
                    modifier = Modifier
                        .padding(bottom = 5.dp)
                        .offset(y = (-30).dp)
                        .offset(x = 10.dp), fontWeight = FontWeight.Bold,
                    color = Color.Black
                )
                Text(
                    text = "剩余筹码: \n$${rememberRemainingChips.value}",
                    modifier = Modifier
                        .padding(bottom = 5.dp)
                        .offset(x = 150.dp)
                        .offset(y = (-40).dp),
                    fontWeight = FontWeight.Bold,
                    color = Color.Magenta
                )
            }
        }
    }

}
